package net.dgg.zz.common.systemRecord.controller;

import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.datatable.DataTableUtils;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.tmd.foundation.platform.org.service.OrganizationManager;
import net.dgg.zz.common.systemRecord.entity.SystemRecord;
import net.dgg.zz.common.systemRecord.service.SystemRecordService;
import net.dgg.zz.common.systemRecord.vo.SystemRecordVO;
import net.dgg.zz.common.utils.DataTableResponse;
import net.dgg.zz.common.utils.DateUtils;
import net.dgg.zz.common.utils.OrderHandleUtils;
import net.dgg.zz.common.utils.excel.ExcelUtils;
import net.dgg.zz.credential.certificateType.service.CertificateTypeService;
import net.fblock.log.NLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 操作日志
 *
 * @Author:HDC
 * @Date: Created in 2018/8/15 10:32
 * @Copyright © dgg group.All Rights Reserved. 版权信息
 */
@Controller
@RequestMapping("sys_record/")
public class SystemRecordController extends DggBaseController {

    @Autowired
    private SystemRecordService systemRecordService;
    @Autowired
    private OrganizationManager organizationManager;
    @Autowired
    private CertificateTypeService certificateTypeService;

    /**
     * 操作日志列表页面
     *
     * @return
     */
    @NLog()
    @RequestMapping("index")
    public String sysRecordPage() {
        return "zzsc/systemRecord/index";
    }


    /**
     * 操作日志数据
     *
     * @param params
     * @return
     */
    @NLog()
    @ResponseBody
    @RequestMapping("sys_record_data")
    public DggRestResponse sysRecordData(@RequestParam Map params, HttpServletRequest request) {
        DataTableRequest r = DataTableUtils.getParam(request); //获取datatable参数
        params.put("page", r.getPage());//获取当前页
        params.put("limit", r.getLength());//获取每页条数
        Map fileMap = new HashMap();
        fileMap.put("operateTime", "operate_time");
        OrderHandleUtils.getOrderCol(r, params, fileMap);
        List<SystemRecord> systemRecordList = systemRecordService.findAllWithPage(params);
        return new DataTableResponse().data(params, systemRecordList);
    }

    /**
     * 导出
     *
     * @param params
     * @return
     */
    @NLog()
    @RequestMapping("export_sys_record")
    public void exportSysRecord(@RequestParam Map params, HttpServletResponse response) {
        try {
            String fileName = "操作日志_" + DateUtils.dateString(new Date(), "yyyyMMddHHmm") + ".xlsx";
            List<SystemRecordVO> dataList = systemRecordService.findByParamsList(params);
            ExcelUtils.writeToFile(dataList, null, fileName, response,SystemRecordVO.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 证书操作日志列表页面
     *
     * @return
     */
    @NLog()
    @RequestMapping("credential_index")
    public String credentialSysRecordPage(Model model) {
        return "zzsc/systemRecord/credential_index";
    }

    /**
     * 证书操作日志数据
     *
     * @param params
     * @return
     */
    @NLog()
    @ResponseBody
    @RequestMapping("credential_sys_record_data")
    public DggRestResponse credentialSysRecordData(@RequestParam Map params, HttpServletRequest request) {
        DataTableRequest r = DataTableUtils.getParam(request); //获取datatable参数
        params.put("page", r.getPage());//获取当前页
        params.put("limit", r.getLength());//获取每页条数
        Map fileMap = new HashMap();
        fileMap.put("operateTime", "operate_time");
        OrderHandleUtils.getOrderCol(r, params, fileMap);
        List<SystemRecord> systemRecordList = systemRecordService.findCredentialWithPage(params);
        return new DataTableResponse().data(params, systemRecordList);
    }

}
